extends Node2D

func _ready():
	set_camera_limits()
	Input.set_custom_mouse_cursor(load("res://assets/ui/crossair_red.png"), Input.CURSOR_ARROW, Vector2(16, 16))
	
	$Player.map = $Ground
	
func set_camera_limits():
	var map_rect = $Ground.get_used_rect()
	var cell_size = $Ground.cell_size
	
	$Player/Camera2D.limit_left = map_rect.position.x * cell_size.x
	$Player/Camera2D.limit_top = map_rect.position.y * cell_size.y
	$Player/Camera2D.limit_right = map_rect.end.x * cell_size.x
	$Player/Camera2D.limit_bottom = map_rect.end.y * cell_size.y

func _on_Tank_shoot(bullet, _position, _direction, _target):
	var b = bullet.instance()
	add_child(b)
	b.start(_position, _direction, _target)


func _on_Player_dead():
	yield(get_tree().create_timer(0.4), "timeout")
	GLOBALS.restart()
	

var tanks: Array = []
var trail := preload("res://effects/Trail.tscn")
var trails: Array = []
func _give_tank_trail(tank):
	#效果很差，暂时取消这个功能
	return
	tanks.append(tank)
	var t = trail.instance()
	$Trails.add_child(t)
	trails.append(t)
	t.start(tank.position)

#更新轨迹
func _on_Timer_timeout():
	for i in range(tanks.size() - 1, -1, -1):
		if is_instance_valid(tanks[i]):
			trails[i]._update_trail(tanks[i].global_position)
		else:
			tanks.remove(i)
			trails.remove(i)

